<?php
class Admin_index extends CI_Controller {
	protected $data = array();
	
	public function __construct()
	{
		parent::__construct();		
		session_start();
	}
	
	public function index()
	{		
		if(empty($_SESSION["my_admin_id"]))	{		
			//set language		
			@$language = !empty($_GET["lang"]) ? strip_tags($_GET["lang"]) : $_SESSION["lang2"];
			$def_lang = "vi";
			if($language == "en"){
				$_SESSION["lang2"] = "en";	
			}else{
				$_SESSION["lang2"] = $def_lang;
			}
			@$language = $_SESSION["lang2"];
			
			include "./common/admin/lang/{$language}.php";
			include "./common/setting.php";			
			include "./common/function.php";
			
			$this->data["siteName"] = $siteName;
			$this->data["lang"] = $lang;
			$this->data["language"] = $language;
			
			$msg = "";
			if(!empty($_GET)){
				if(isset($_GET["msg"])){
					$msg = $_GET["msg"];
				}
			}
			$errmsg = "";
			if (!empty($msg)) {
				switch ($msg) {
					case 1:
						$errmsg = $lang["index"]["errmsg1"];
						break;
					case 2:
						$errmsg = $lang["index"]["errmsg2"];
						break;
					case 3:
						$errmsg = $lang["index"]["errmsg3"];
						break;
					case 4:
						$errmsg = $lang["index"]["errmsg4"];
						break;
					case 5:
						$errmsg = $lang["index"]["errmsg5"];
						break;
					case 6:
						$errmsg = $lang["index"]["errmsg6"];
						break;
				}
			}
						
			if (!empty($errmsg)) {
				$errmsg = "<div align='center'><font color='red'>" . $errmsg . "</font></div>";
			}
						
			$langOption = array_single_option($arr_lang, $language, "lang2", "");
			$userEmail = !empty($_GET["id"]) ? $_GET["id"] : "";
			
			$this->data["errmsg"] = $errmsg;
			$this->data["userEmail"] = $userEmail;
			$this->data["langOption"] = $langOption;
			$this->load->view('admin/index', $this->data);
		}
		else {
			include "./common/admin/access.php";
			$this->data["siteName"] = $siteName;
			$this->data["lang"] = $lang;
			$this->data["language"] = $language;
			
			$this->load->view('admin/index2', $this->data);
		}
	}
	
	public function login()
	{	
		@$userID = !empty($_POST["userEmail"]) ? $_POST["userEmail"] : "";
		@$userPass = !empty($_POST["userPass"]) ? $_POST["userPass"] : "";
		@$lang2 = !empty($_POST["lang2"]) ? $_POST["lang2"] : "vn";
		
		$userID = strip_tags(htmlspecialchars($userID));
		$userPass = strip_tags(htmlspecialchars($userPass));
		if(empty($userID)){
			session_destroy();
			session_start();
			$_SESSION["lang2"] = $lang2;
			header("Location: " . base_index . "admin?msg=1");
			exit();
		}
		
		include "./common/admin/lang/{$lang2}.php";
		include "./common/setting.php";
		$this->load->model('admin_userModel');
		
		if(empty($userPass)){
			session_destroy();
			session_start();
			$_SESSION["lang2"] = $lang2;
			header("Location: " . base_index . "admin?msg=2&id=$userID");
			exit();
		}		
		
		$query = $this->admin_userModel->getUser(array("userLogin" => $userID));
		if($query->num_rows < 1){
			session_destroy();
			session_start();
			$_SESSION["lang2"] = $lang2;
			header("Location: " . base_index . "admin?msg=3");
			exit();
		} else {
			$user = $query->result();
			$user = $user[0];
			$check_userID = $user->userID;
			$check_pass = $user->userPass;
			$userName = $user->userName;
			$query->free_result();
			
			if ($check_pass == $userPass) {
				$_SESSION['my_admin_id'] = $check_userID;
				$_SESSION['admin_name'] = $userName;
				$_SESSION["lang2"] = $lang2;				
				header("Location: " . base_index . "admin/");
				exit();
			} else {
				session_destroy();
				session_start();
				$_SESSION["lang2"] = $lang2;
				header("Location: " . base_index . "admin?msg=4&id=$userID");
				exit();
			}
		}
	}
	
	public function logout()
	{	
		//set language
		@$language = !empty($_GET["lang"]) ? strip_tags($_GET["lang"]) : $_SESSION["lang2"];
		if($language == "en"){
			$_SESSION["lang2"] = "en";	
		}else{
			$_SESSION["lang2"] = "vi";
		}		
		$language = $_SESSION["lang2"];		
		include "./common/admin/lang/{$language}.php";
		include "./common/setting.php";
		session_destroy();
		
		$this->data["siteName"] = $siteName;
		$this->data["lang"] = $lang;
		$this->data["language"] = $language;
		$this->data["language"] = $language;
		$this->load->view('admin/logout', $this->data);
	}
	
	public function changeExt()
	{
		/*
		$dir = realpath(dirname(__FILE__));
		$folder = "admin";
		$path = $dir . "/../views/" . $folder . "/";
		//$path = $dir . "/../../_files2/";
		if (is_dir($path))		
		{
			$dir_handle = opendir($path);
			if (!$dir_handle)
				return;
			while($file = readdir($dir_handle)) {
				if ($file != "." && $file != "..") {
					$ext = end(explode(".", $file));				
					if($ext == "phtml" || $ext == "html"){					
						$name = str_replace("." . $ext, "", $file);
						rename($path . $file, $path . $name . "." . "php");
					}				
				}
			}
	  	}
		/**/
	}
}